/**
 * 分页 Composable
 */
import { ref } from "vue"
import { PAGINATION } from "@/constants/ui"

/**
 * 分页工具函数
 * @param {number} initialPage - 初始页码
 * @param {number} initialPageSize - 初始每页大小
 * @returns {Object}
 */
export function usePagination(
  initialPage = PAGINATION.DEFAULT_PAGE_NUM,
  initialPageSize = PAGINATION.DEFAULT_PAGE_SIZE
) {
  const pageNum = ref(initialPage)
  const pageSize = ref(initialPageSize)
  const total = ref(0)

  /**
   * 处理分页变化
   */
  const handlePagination = (event) => {
    pageNum.value = event.pageNum
    pageSize.value = event.pageSize
  }

  /**
   * 设置总数
   */
  const setTotal = (count) => {
    total.value = count
  }

  /**
   * 重置到第一页
   */
  const reset = () => {
    pageNum.value = 1
  }

  /**
   * 获取当前分页参数
   */
  const getParams = () => {
    return {
      pageNum: pageNum.value,
      pageSize: pageSize.value
    }
  }

  return {
    pageNum,
    pageSize,
    total,
    handlePagination,
    setTotal,
    reset,
    getParams
  }
}
